package ModelLayer;
import java.util.ArrayList;
import java.util.Iterator;
/**
 * The class AddressBook stores object of the Person class.
 * 
 * @author (Gruppe B) 
 * @version (1.0)
 */
public class AddressBook
{
    /**
     * Instance variables.
     */
        private static AddressBook instance;
        private ArrayList<Person> persons;

    /**
     * Constructor for addressBook object.
     */
    private AddressBook()
    {
        persons = new ArrayList<Person>();
    }

    /**
     * Singleston.
     */
    public static AddressBook getInstance()
    {
        if (instance == null)
        {
            instance = new AddressBook();
        }
        return instance;
    }
    
    /**
     * Returns the information about a specific person from the id.
     */
    public void readPerson(int id)
    {
        for(Person person : persons)
        {
            int personId = id;
            if(personId == person.getId())
            {
                person.printPersonInfo();
            }
        }
        
    }
    
    /**
     * Delets the person form the arraylist using id.
     */
    public void deletePerson(int id)
    {
        Iterator<Person> it = persons.iterator();
        while (it.hasNext()){
            Person p = it.next();
            int secondId = p.getId();
            if(secondId == id)
            {
                readPerson(id);
                System.out.println("Has been deleted");
                p.getId();                
                it.remove();
            }
        }
    }
    
    /**
     * Updates the information about a person using the id.
     */
    public void updatePerson(int id, String name, String address, String postalCode, String city, String phone)
    {
        Iterator<Person> it = persons.iterator();
        while (it.hasNext()){
            Person p = it.next();
            int secondId = p.getId();
            if(secondId == id)
            {                
                p.setName(name);
                p.setAddress(address);
                p.setPostalCode(postalCode);
                p.setCity(city);
                p.setPhone(phone);                
            }
        }        
    }
    
    /**
     * Adds a person to the arraylist.
     */
    public void addPerson(Person person)
    {
        persons.add(person);
    }
    
    /**
     * Prints out all the information about all the persons in the arraylist.
     */
    public void listPersons()
    {
        for( Person person : persons )
        {
            person.printPersonInfo();
        }        
    }
}
